﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Mbs.ComponentModel;
using Mbs.Globalization;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;

namespace Mbs.Test.Globalization
{
    [TestFixture]
    public class LocalObjectTest
    {
        class Employee : UndoableObject
        {
            private string name;
            private int age;

           
            public bool Sex { get; set; }

            [LocalizedProperty(Name = "${res:Employee.Name}", Category = "${res:Employee.Category}", Description = "${res:Employee.Name.Description}")]
            public string Name
            {
                get { return name; }
                set
                {
                    if (!string.Equals(value, name))
                    {
                        var old = name;
                        name = value;
                        OnPropertyChanged("Name", value, old);
                    }
                }
            }

            [LocalizedProperty(Name = "${res:Employee.Age}", Category = "${res:Employee.Category}", Description = "${res:Employee.Age.Description}")]
            public int Age
            {
                get { return age; }
                set
                {
                    if (value != age)
                    {
                        var old = age;
                        age = value;
                        OnPropertyChanged("Age", value, old);
                    }
                }
            }
        }

        string initLanguage;
        [SetUp]
        public void Init()
        {
            initLanguage = "en";
            LanguageManager.Instance.Language = initLanguage;
        }

        [TearDown]
        public void Release()
        {
            LanguageManager.Instance.Language = initLanguage;
        }

        ManualResetEvent mre;
        const string Title = "How are you!";
        Form mainForm;
        PropertyGrid grid = new PropertyGrid();

        [Test]
        public void Test()
        {
            var strResourceMgr = ResourceRepository.StringRegistry;
            Assert.IsNotNull(strResourceMgr);

            strResourceMgr.Register("Mbs.Test.Data.Resources", Assembly.GetExecutingAssembly());


            mre = new ManualResetEvent(false);
            mainForm = new Form();
           
            mainForm.Controls.Add(grid);
            grid.Dock = DockStyle.Fill;
            grid.SelectedObject = new Employee { Name = "Zhang San", Age = 18 };

            new Thread(() => RunMessageLoop() ){ Name = "UI", ApartmentState = ApartmentState.STA }.Start();


            mre.WaitOne(16 * 1000);
            mre.Close();
        }


        void RunMessageLoop()
        {

            //Action setTitle =()=> SetTitle(mainForm);

            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Interval = 1000;

            int counter = 0;
            timer.Tick += (s, e) =>
            {
                counter++;
                if (counter == 10)
                {
                    LanguageManager.Instance.Language = "zh-cn";
                    grid.SelectedObject = grid.SelectedObject;
                    grid.Invalidate();
                }
                else if(counter == 15)
                {
                    timer.Stop();
                    timer.Dispose();
                                     

                    mainForm.Close();
                    mre.Set();
                }
            };

         
            mainForm.Shown += (s, e) => timer.Start();
         
            Application.Run(mainForm);
        }


    }
}
